/****************************************************
          File Name:sem_open.c
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Sat 20 Sep 2014 06:17:09 PM CST
*****************************************************/

#include"posix_ipc.h"
#include<getopt.h>

void usage(char *arg)
{
    printf("%s /sem\n",arg);
    exit(EXIT_FAILURE);
}

int main(int argc, char *argv[])
{
    sem_t *sem;
    int opt;
    int flag=O_CREAT;

	Usage(argc,"/sem",argv[0],argv[1]);

    while((opt=getopt(argc,argv,"x"))!=-1)
    {
        switch(opt)
        {
            case 'x':
                flag|=O_EXCL;
                break;
            default:
                break;
        }
    }

    sem=sem_open(argv[optind],flag,MODE,1);    /* semaphore initailized to 1 */
    if(sem==SEM_FAILED)
        err("sem_open");

    printf("sem: %d\n",*sem);
    exit(EXIT_SUCCESS);
}

